package com.rede.didiok.common.enums;

import java.util.HashMap;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;

import com.rede.didiok.common.constant.conf.base.BaseSysConf;
import com.rede.didiok.common.utils.JsonUtils;

/**
 * 行为枚举类
 *
 * @author 陌溪
 * @date 2020/9/14 10:40
 */
@Slf4j
public enum BehaviorEnum {

    /**
     * 用户行为
     */
    RANK("点击榜单", "rank"),
    RANK_CATEGORY("点击榜单分类", "rank_category"),
    RANK_MEMBER_GRADE("点击榜单成员等级", "rank_member_grade"),
    RANK_MEMBER_CONTENT("点击榜单成员", "rank_member_content"),
    RANK_MEMBER_PRAISE("点赞", "rank_member_praise"),
    RANK_MEMBER_SEARCH("点击搜索", "rank_member_search"),
    RANK_MEMBER_USER_NAME("点击创建人", "rank_member_user_name"),

    FRIENDSHIP_LINK("点击友情链接", "friendship_link"),
    STUDY_VIDEO("点击学习视频", "study_video"),
    VISIT_PAGE("访问页面", "visit_page"),
    CLICK_BUTTON("点击按钮", "clickButton"),
    VISIT_CLASSIFY("点击博客分类", "visit_classify"),
    VISIT_SORT("点击归档", "visit_sort"),

    LOADING_VERIFY("加载校验", "loading_verify"),
    PUBLISH_MEMBER_COMMENT("发表榜单成员评论", "publish_member_comment"),
    PUBLISH_MOMENT_COMMENT("发表用户动态评论", "publish_moment_comment"),
    DELETE_MEMBER_COMMENT("删除榜单成员评论", "delete_member_comment"),
    DELETE_MOMENT_COMMENT("删除用户动态评论", "delete_moment_comment"),
    REPORT_MEMBER_COMMENT("举报榜单成员评论", "report_member_comment"),
    REPORT_MOMENT_COMMENT("举报用户动态评论", "report_moment_comment"),
    VISIT_GRADE("点击榜单成员等级页面", "visit_grade"),
    COLLECT("收藏", "collect"),
    CANCEL_COLLECT("取消收藏", "cancel_collect"),
    RANK_PRAISE("点赞", "rank_praise"),
    COMMUNITY_PRAISE("点赞", "community_praise"),
    CANCEL_RANK_PRAISE("取消榜单点赞", "cancel_rank_praise"),
    CANCEL_COMMUNITY_PRAISE("取消社区点赞", "cancel_community_praise"),
    RANK_TREAD("点踩", "rank_tread"),
    COMMUNITY_TREAD("点踩", "community_tread"),
    CANCEL_RANK_TREAD("取消点踩", "cancel_rank_tread"),
    CANCEL_COMMUNITY_TREAD("取消点踩", "cancel_community_tread"),

    ADD_RANK_MEMBER("增加榜单成员", "addRankMember"),
    PUBLISH_RANK_MEMBER("发布/下架博客", "publishRankMember"),
    EDIT_RANK_MEMBER("编辑榜单成员", "editRankMember"),
    DELETE_RANK_MEMBER("删除榜单成员", "deleteRankMember"),
    GET_NOTICE("获取通知", "getNotice"),
    DELETE_NOTICE("删除通知", "deleteNotice"),
    DELETE_BATCH_NOTICE("删除选中通知", "deleteBatchNotice"),
    REPORT("举报", "report"),
    SIGN_IN("签到", "signIn"),
    RETROACTIVE_SIGN_IN("签到", "retroactiveSignIn"),
    UPDATE_BACKGROUND_IMAGE("更新背景图片", "updateBackgroundImage"),
    ADD_MOMENT("发表动态", "addMoment"),
    DELETE_MOMENT("删除动态", "deleteMoment"),
    PARSE_URL("解析URL信息", "parseUrl"),
    REPLY_MOMENT("动态回复", "add_moment"),


    SEARCH_USER_LIST("搜索用户列表", "search_user_list"),
    CHANGE_SEARCH_TYPE("切换外部搜索方式", "change_search_type"),
    SUBMIT_OUTSIDE_SEARCH("外部搜索", "submit_outside_search"),
    CHECK_CURRENT_USER("校验当前用户是否登录", "check_current_user"),
    ;

    private String content;
    private String behavior;

    BehaviorEnum(String content, String behavior) {
        this.content = content;
        this.behavior = behavior;
    }

    /**
     * 根据value返回枚举类型，主要在switch中使用
     *
     * @param value
     * @return
     */
    public static BehaviorEnum getByValue(String value) {
        for (BehaviorEnum behavior : values()) {
            if (behavior.getBehavior() == value) {
                return behavior;
            }
        }
        return null;
    }

    public static Map<String, String> getModuleAndOtherData(BehaviorEnum behavior,
                                                            Map<String, Object> nameAndArgsMap,
                                                            String bussinessName) {
        String otherData = "";
        String moduleUid = "";
        String bizType = ""; // 业务类型
        switch (behavior) {
            case RANK_MEMBER_USER_NAME: {
                // 判断是否是点击作者
                if (nameAndArgsMap.get(BaseSysConf.CREATE_USER_NAME) != null) {
                    otherData = nameAndArgsMap.get(BaseSysConf.CREATE_USER_NAME).toString();
                }
            }
            break;
            case RANK_CATEGORY: {
                // 判断是否点击博客分类
                if (nameAndArgsMap.get(BaseSysConf.CAT_UID) != null) {
                    moduleUid = nameAndArgsMap.get(BaseSysConf.CAT_UID).toString();
                }
            }
            break;
            case RANK_MEMBER_GRADE:{
                // 判断是否点击博客标签
                if (nameAndArgsMap.get(BaseSysConf.TAG_UID) != null) {
                    moduleUid = nameAndArgsMap.get(BaseSysConf.TAG_UID).toString();
                }
            }
            break;
            case RANK_MEMBER_SEARCH: {
                // 判断是否进行搜索
                if (nameAndArgsMap.get(BaseSysConf.KEYWORDS) != null) {
                    otherData = nameAndArgsMap.get(BaseSysConf.KEYWORDS).toString();
                }
            }
            break;

            case VISIT_SORT: {
                // 判断是否点击归档
                if (nameAndArgsMap.get(BaseSysConf.MONTH_DATE) != null) {
                    otherData = nameAndArgsMap.get(BaseSysConf.MONTH_DATE).toString();
                }
            }
            break;
            case RANK_MEMBER_CONTENT: {
                // 判断是否博客详情
                if (nameAndArgsMap.get(BaseSysConf.UID) != null) {
                    moduleUid = nameAndArgsMap.get(BaseSysConf.UID).toString();
                } else if (nameAndArgsMap.get(BaseSysConf.OID) != null) {
                    // 当收到的是oid的时候，存储到otherData处
                    otherData = nameAndArgsMap.get(BaseSysConf.OID).toString();
                }
            }
            break;
            case RANK_MEMBER_PRAISE: {
                // 判断是否给博客点赞
                if (nameAndArgsMap.get(BaseSysConf.UID) != null) {
                    moduleUid = nameAndArgsMap.get(BaseSysConf.UID).toString();
                }
            }
            break;
            case FRIENDSHIP_LINK: {
                // 判断是否点击友链
                if (nameAndArgsMap.get(BaseSysConf.UID) != null) {
                    moduleUid = nameAndArgsMap.get(BaseSysConf.UID).toString();
                }
                otherData = bussinessName;
            }
            break;
            case VISIT_PAGE: {
                // 访问页面
                if (nameAndArgsMap.get(BaseSysConf.PAGE_NAME) != null) {
                    otherData = nameAndArgsMap.get(BaseSysConf.PAGE_NAME).toString();
                } else {
                    otherData = bussinessName;
                }
            }
            break;
            case PUBLISH_MEMBER_COMMENT: {
                // 发表评论
                Object object = nameAndArgsMap.get(BaseSysConf.MEMBER_COMMENT_DTO);
                Map<String, Object> map = JsonUtils.objectToMap(object);
                if (map.get(BaseSysConf.CONTENT) != null) {
                    otherData = map.get(BaseSysConf.CONTENT).toString();
                }
            }
            break;
            case REPORT_MEMBER_COMMENT: {
                // 举报评论
                Object object = nameAndArgsMap.get(BaseSysConf.MEMBER_COMMENT_DTO);
                Map<String, Object> map = JsonUtils.objectToMap(object);
                if (map.get(BaseSysConf.CONTENT) != null) {
                    otherData = map.get(BaseSysConf.CONTENT).toString();
                }
            }
            break;
            case DELETE_MEMBER_COMMENT: {
                // 删除评论
                Object object = nameAndArgsMap.get(BaseSysConf.MEMBER_COMMENT_DTO);
                Map<String, Object> map = JsonUtils.objectToMap(object);
                if (map.get(BaseSysConf.CONTENT) != null) {
                    otherData = map.get(BaseSysConf.CONTENT).toString();
                }
            }
            break;
            case LOADING_VERIFY: {
                // 删除评论
                Object object = nameAndArgsMap.get(BaseSysConf.VALID_CODE);
                if (object != null) {
                    otherData = object.toString();
                }
            }
            break;
            case COLLECT:
            case CANCEL_COLLECT: {
                // 收藏内容
                Object object = nameAndArgsMap.get(BaseSysConf.COLLECT_DTO);
                Map<String, Object> map = JsonUtils.objectToMap(object);
                if (map.get(BaseSysConf.BIZ_UID) != null) {
                    moduleUid = map.get(BaseSysConf.BIZ_UID).toString();
                }
                if (map.get(BaseSysConf.COLLECT_TYPE) != null) {
                    otherData = map.get(BaseSysConf.COLLECT_TYPE).toString();
                }
            }
            break;
            case RANK_TREAD:
            case CANCEL_RANK_TREAD:
            case RANK_PRAISE:
            case CANCEL_RANK_PRAISE: {
                // 点赞内容
                Object object = nameAndArgsMap.get(BaseSysConf.RANK_PRAISE_RECORD_DTO);
                Map<String, Object> map = JsonUtils.objectToMap(object);
                if (map.get(BaseSysConf.RESOURCE_UID) != null) {
                    moduleUid = map.get(BaseSysConf.RESOURCE_UID).toString();
                }
                if (map.get(BaseSysConf.RESOURCE_Type) != null) {
                    otherData = map.get(BaseSysConf.RESOURCE_Type).toString();
                }
            }
            break;
            case COMMUNITY_TREAD:
            case CANCEL_COMMUNITY_TREAD:
            case COMMUNITY_PRAISE:
            case CANCEL_COMMUNITY_PRAISE: {
                // 点赞内容
                Object object = nameAndArgsMap.get(BaseSysConf.COMMUNITY_PRAISE_RECORD_DTO);
                Map<String, Object> map = JsonUtils.objectToMap(object);
                if (map.get(BaseSysConf.RESOURCE_UID) != null) {
                    moduleUid = map.get(BaseSysConf.RESOURCE_UID).toString();
                }
                if (map.get(BaseSysConf.RESOURCE_Type) != null) {
                    otherData = map.get(BaseSysConf.RESOURCE_Type).toString();
                }
            }
            break;

            /**
             * 创作博客相关
             */
            case ADD_RANK_MEMBER:
            case EDIT_RANK_MEMBER:
            case PUBLISH_RANK_MEMBER:
            case DELETE_RANK_MEMBER: {
                // 问答
                Object object = nameAndArgsMap.get(BaseSysConf.RANK_MEMBER_DTO);
                Map<String, Object> map = JsonUtils.objectToMap(object);
                if (map.get(BaseSysConf.UID) != null) {
                    moduleUid = map.get(BaseSysConf.UID).toString();
                }
                if (map.get(BaseSysConf.TITLE) != null) {
                    otherData = map.get(BaseSysConf.TITLE).toString();
                }
            }
            break;

            /**
             * 举报相关
             */
            case REPORT: {
                // 举报内容
                Object object = nameAndArgsMap.get(BaseSysConf.REPORT_DTO);
                Map<String, Object> map = JsonUtils.objectToMap(object);
                if (map.get(BaseSysConf.REPORT_CONTENT_UID) != null) {
                    moduleUid = map.get(BaseSysConf.REPORT_CONTENT_UID).toString();
                }
                if (map.get(BaseSysConf.CONTENT) != null) {
                    otherData = map.get(BaseSysConf.CONTENT).toString();
                }
            }
            break;

            /**
             * 用户动态相关
             */
            case ADD_MOMENT:
            case DELETE_MOMENT: {
                // 用户动态相关
                Object object = nameAndArgsMap.get(BaseSysConf.USER_MOMENT_DTO);
                Map<String, Object> map = JsonUtils.objectToMap(object);
                if (map.get(BaseSysConf.CONTENT) != null) {
                    otherData = map.get(BaseSysConf.CONTENT).toString();
                }
            }
            break;


            default: {
                log.info("其它行为");
            }
        }
        Map<String, String> result = new HashMap<>();
        result.put(BaseSysConf.MODULE_UID, moduleUid);
        result.put(BaseSysConf.OTHER_DATA, otherData);
        return result;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getBehavior() {
        return behavior;
    }

    public void setBehavior(String behavior) {
        this.behavior = behavior;
    }


}